<?php

/*	A paging/sorting class	*/

if (!isset($_GET['frompage'])) {
	$_GET['frompage'] = '';
}

class Paging {
	var $query,
		$anchor=true,
		$headers,
		$sortby,
		$sorttype,
		$length=-1,
		$frompage=0,
		$itemsperpage,
		$pagesperpage=10;

	function Paging($query, $sortby=1, $sorttype=false, $headers=array(), $itemsperpage=10) {
		if (count($headers)>0&&$sortby&&!isset($headers[$sortby])) {
			list($key,) = each($headers);
			$sortby =& $key;
		}
		reset($headers);
		$this->query = preg_replace("#^(SELECT)(.*?)(ORDER BY(.*))?#is", "$1 SQL_CALC_FOUND_ROWS $2", $query);
		$this->sortby = $sortby;
		$this->sorttype = $sorttype;
		$this->headers = $headers;
		$this->itemsperpage = $itemsperpage;
		if ($_GET['frompage'])	$this->frompage = (int)$_GET['frompage'];
	}

	function count() {
		if ($this->length<0) {
			$row = mysql_fetch_object(query("SELECT FOUND_ROWS() length"));
			return (int)$row->length;
		}
		else
			return (int)$this->length;
	}

	function query() {
		$result = ($this->sortby)?query($this->query." ORDER BY $this->sortby ".(($this->sorttype)?"DESC ":"").", 1 LIMIT $this->frompage, $this->itemsperpage"):query($this->query." LIMIT $this->frompage, $this->itemsperpage");
		$this->length = $this->count();
		return $result;
	}

	function display($onclick="", $show_total=false) {
    $PHP_SELF = $_SERVER['PHP_SELF'];
    $QUERY_STRING = $_SERVER['QUERY_STRING'];
    
		if ($onclick) $onclick = " ".$onclick;
		$QUERY_STRING = ltrim(preg_replace("/(?:&?frompage=(?:[0-9]+)?((&){2,})?)/", "\\1", $QUERY_STRING), "&");
		echo "<div class=\"paging_pages\"><div class=\"pages\">";
		$query_string = ($QUERY_STRING)?"&$QUERY_STRING":"";
		if ($show_total && $this->anchor) {
			echo "<span class=\"show_total\">";
			$this->show_total();
			echo "</span>";
		}
		if ($this->length>$this->itemsperpage) {
			echo "<ul class=\"paging\">";
			if (!$this->anchor) echo "<a name=\"paging\"></a>";
      echo ($this->frompage!=0)?"<li><a rel=\"nofollow\" href=\"$PHP_SELF?frompage=".($this->frompage-$this->itemsperpage)."$query_string\"$onclick class=\"links\"> &laquo; </a></li>":"<li><span class=\"nolink\"> &laquo; </span></li>";
			$start = (1+($this->frompage/$this->itemsperpage)*2 <= $this->pagesperpage || ceil($this->length/$this->itemsperpage) <= $this->pagesperpage)?1:ceil(1+($this->frompage/$this->itemsperpage)-($this->pagesperpage/2));
			$end = ($start+$this->pagesperpage-1 < ceil($this->length/$this->itemsperpage))?$start + $this->pagesperpage-1:ceil($this->length/$this->itemsperpage);
			if ($end-$start < $this->pagesperpage-1 && ceil($this->length/$this->itemsperpage) > $this->pagesperpage) $start = $start - ($this->pagesperpage - $end + $start);
      for ($i=$start; $i<=$end; $i++) {
        echo ($i!=$this->frompage/$this->itemsperpage+1)?"<li><a rel=\"nofollow\" href=\"$PHP_SELF?frompage=".(($i-1)*$this->itemsperpage)."$query_string\"$onclick class=\"links\">$i</a></li>":"<li><span class=\"nolink\">$i</span></li>";
      }
			echo ($this->length>($this->frompage+$this->itemsperpage))?"<li><a rel=\"nofollow\" href=\"$PHP_SELF?frompage=".($this->frompage+$this->itemsperpage)."$query_string\"$onclick class=\"links\"> &raquo; </a></li>":"<li><span class=\"nolink\"> &raquo; </span></li>";
			echo "</ul>";
    }
		if ($show_total && !$this->anchor) {
			echo "<span class=\"show_total\">";
			$this->show_total();
			echo "</span>";
		}
		$this->anchor = true;
		echo "</div></div>";
	}

	function show_total() {
		$topage = $this->frompage+$this->itemsperpage;
		if ($topage>$this->length) $topage = $this->length;
		echo (($this->length)?$this->frompage+1:$this->frompage)." - $topage от $this->length";
	}

	function displayNext($s="") {
		
		if (!(list($key, $value)=each($this->headers))) {
			reset($this->headers);
			list($key, $value) = each($this->headers);
		}
		$clean_query = preg_replace("/&?sortBy=[0-9]*&sort=[0-1]?&?$/", "", $_SERVER['QUERY_STRING']);
    
		echo (($this->sortby==$key)?(($this->sorttype)?"<img src=\"images/arrow_up.gif\" width=\"11\" height=\"9\" />":"<img src=\"images/arrow_down.gif\" width=\"11\" height=\"9\" />"):"<img src=\"images/px.gif\" width=\"11\" height=\"9\" />")." <a rel=\"nofollow\" href=\"{$_SERVER['PHP_SELF']}?".$clean_query.($clean_query?"&":"")."sortBy=$key&sort=".(($this->sortby!=$key)?0:(!$this->sorttype))."\" >$value</a>$s";
	}

	
}
?>